package com.mall.web.control.app.mallGroupBuy;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.mall.biz.comment.MallCommentBizImpl;
import com.mall.biz.home.HomeBizImpl;
import com.mall.client.comment.MallCommentService;
import com.mall.client.mall.MallService;
import com.mall.client.mallGroupBuy.MallGroupbBuyService;
import com.mall.common.util.PageUtil;
import com.mall.pojo.Mall;
import com.mall.pojo.MallGroupBuy;
import com.mall.query.MallGroupBuyQuery;
import com.mall.query.Page;
import com.mall.query.TuanDetailQuery;
import com.mall.vo.home.MallGroupBuyVO;
import com.mall.vo.user.AliTonghua;
import com.mall.vo.user.MallCommentVO;
import com.mall.web.util.APPCookieUtil;
import com.mall.web.util.HttpUtil;
import com.mall.web.vo.ResponseEntity;



@Controller
@RequestMapping("/mallGroupBuy/")
public class MallGroupBuyController {
	@Autowired
	private  HomeBizImpl homeBiz;
	@Autowired
	private MallGroupbBuyService mallGroupbBuyService;
	@Autowired
	private MallService mallService;
	@Autowired
	private MallCommentService mallCommentService;
	@Autowired
	private MallCommentBizImpl mallCommentBiz;
	
	@RequestMapping("index")
	public String index(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		mallGroupBuyMap(request,response,modelMap);
		return "mallGroupBuy/index";
		
	}
	
	@RequestMapping("indexForApp")
	@ResponseBody
	public ResponseEntity<ModelMap> indexForApp(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		mallGroupBuyMap(request,response,modelMap);
		ResponseEntity<ModelMap> responseEntity = new ResponseEntity<>();
		responseEntity.initSuccResponseEntity(modelMap, "index message");
		return responseEntity;
		
	}
	@RequestMapping("tuanDetail")
	public String tuanDetail(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) {
		int app = APPCookieUtil.urlFromApp(request);
		modelMap.put("app", app);
		Long mallGroupBuyId = HttpUtil.getLongValueFromRequest(request, "mallGroupBuyId");
		MallGroupBuy mallGroupBuy = this.mallGroupbBuyService.getMallGroupBuyById(mallGroupBuyId);
		Mall mall = this.mallService.getMallById(mallGroupBuy.getMallId());

		String tuanDesc = mallGroupBuy.getTuanDesc();
		List<TuanDetailQuery> tuanList = JSONArray.parseArray(tuanDesc, TuanDetailQuery.class);

//		TravelAroundQuery travelAround = null;
//		if (mall.getMallCategoryId() == 7) {
//			String travelTuanDesc = mallGroupBuy.getAttribute();
//			if (StringUtils.isNotBlank(travelTuanDesc)) {
//				JSONObject travelTuanDescJson = JSONObject.fromObject(travelTuanDesc);
//				travelAround = (TravelAroundQuery) JSONObject.toBean(travelTuanDescJson, TravelAroundQuery.class);
//			}
//			modelMap.put("travelAround", travelAround);
//		}
		
		//店家的对话id
		AliTonghua aliTonghua = this.homeBiz.getSellerALiUserInfo(mall);
		modelMap.put("aliTonghua", aliTonghua);
		/**
		 * 团购评论的type是1
		 */
		int mallCommontCount = mallCommentService.countMallCommentListByMallId(mall.getId(), 1, 0);
		List<MallCommentVO> mallCommentVOList = null;
		if (mallCommontCount > 0) {
			mallCommentVOList = this.mallCommentBiz.getMallCommentVOListByMallId(mall.getId(), 0, 1,
					Page.getInitPage(PageUtil.PAGE_SIZE_APP));
		}
		modelMap.put("mallCommontCount", mallCommontCount);
		modelMap.put("mallCommentVOList", mallCommentVOList);

		modelMap.put("mallGroupBuy", mallGroupBuy);
		modelMap.put("mall", mall);
		modelMap.put("tuanList", tuanList);
		String referer = request.getHeader("Referer");
		modelMap.put("referer", referer);
		return "mallGroupBuy/tuanDetail";

	}
	
	
	private  ModelMap mallGroupBuyMap(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap){
		
		Integer onlyKey = HttpUtil.getCommkeyId(request, response);
		String keyWord = HttpUtil.getURLDecoderValueFromRequest(request, "keyWord");
		Integer categoryId = HttpUtil.getIntegerValueFromRequest(request, "categoryId");
		Integer secCategoryId = HttpUtil.getIntegerValueFromRequest(request, "secCategoryId");
		
		int pageNo = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageNo", 1);
		int pageSize = HttpUtil.getIntegerValueHasDefaultFromRequest(request, "pageSize", PageUtil.PAGE_SIZE_MALL);
		Page page = new Page();
		page.setPageByPageNoAndPageSize(pageNo, pageSize);
		MallGroupBuyQuery mallGroupBuyQuery = new MallGroupBuyQuery();
		mallGroupBuyQuery.setOnlyKey(onlyKey);
		mallGroupBuyQuery.setCategoryId(categoryId);
		mallGroupBuyQuery.setSecCategoryId(secCategoryId);
		
		mallGroupBuyQuery.setKeyWord(keyWord);
		mallGroupBuyQuery.setPage(page);
		mallGroupBuyQuery.setDoset(true);
		//首页下面的团购内容
		List<MallGroupBuyVO> mallGroupBuyVOList = homeBiz.getMallGroupBuyVOByQuery(mallGroupBuyQuery);
		modelMap.put("mallGroupBuyVOList", mallGroupBuyVOList);
		return modelMap;
		
	}

}
